<!-- 附件 -->
<link href="${staticPath!}/plugins/uploadify-v3.1/uploadify.css" rel="stylesheet" type="text/css"/>
<script src="${staticPath!}/plugins/uploadify-v3.1/jquery.uploadify-3.1.js" type="text/javascript"></script>
<script type="text/javascript">
    var imgageIds = "";
    var fileCount_attach = 0;
    var fileCountLimit = 20;
    var fileSizeLimit = "524288000";
    var totalFileSizeLimit = "524288000";
    var fileCount = "no";
    $(function () {
        $("#uploadify_attach").uploadify({
            'buttonText': '上传附件',
            'height': 30,
            'removeCompleted': false,
            'method': 'post',
            'langFile': '/plugins/uploadify-v3.1/uploadifyLang_cn.js',
            'swf': '/plugins/uploadify-v3.1/uploadify.swf',
            'cancelImage': '/plugins/uploadify-v3.1/uploadify-cancel.png',
            'uploader': '/systemAttach/uploadAttachToDirAndDb',
            'formData': '',
            'buttonCursor': 'pointer',
            'fileSizeLimit': fileSizeLimit,
            'fileTypeDesc': '文件类型说明',
            'fileTypeExts': "*.txt;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.jpg;*.jpeg;*.gif;*.png;",
            'width': 90,
            'style': 'margin-left:30px',
            'auto': true,
            'multi': true,
            'fileObjName': 'file',
            'successTimeout': '999999',
            'onUploadSuccess': function (file, data, response) {
                if (data == "" || data == "undefined" || data == null) {
                    //上传失败
                    Jmts.alert("文件：" + file.name + "上传失败");
                    $("#uploadify_attach").uploadify('cancel', file.id);
                } else {//上传成功
                    fileCount_attach = fileCount_attach + 1;
                    totalFileSize = totalFileSize + file.size;
                    $("#" + file.id).append("<input type='hidden' value='" + file.name + "' name='successUploadFileName_attach'/>");
                    $("#" + file.id).append("<input type='hidden' value='" + data + "' name='successUploadFileId_attach'/>");
                    if (totalFileSize > totalFileSizeLimit) {
                        fileCount_attach = fileCount_attach - 1;
                        totalFileSize = totalFileSize - file.size;
                        Jmts.alert("上传附件总大小超过限制" + parseInt(totalFileSizeLimit / 1024 / 1024 + "MB"));
                        $("#" + file.id).nextAll().each(function (index) {
                            handleCancelIduploadify_attach($(this).attr("id"), null, false);
                            $("#uploadify_attach").uploadify('cancel', $(this).attr("id"));
                        });

                        $("#uploadify_attach").uploadify('stop', file);
                        $("#uploadify_attach").uploadify('cancel', file.id);
                        handleCancelIduploadify_attach(file.id, file.size, false);
                        var deletedId = $("#" + file.id).find("input[name='successUploadFileId_attach']");
                        $("#deletedIds_attach").val($("#deletedIds_attach").val() + deletedId.val() + ",");
                        return;
                    }
                }
            },
            'onQueueComplete': function (data) {
                var $filesId = $("input[name='successUploadFileId_attach']");
                var $filesName = $("input[name='successUploadFileName_attach']");
                var ids = "";
                var names = "";
                for (var i = 0; i < $filesId.length; i++) {
                    ids += $filesId[i].value;
                    //var fileName = $($filesId[i]).parent().find(".fileName").text();
                    //names= names + fileName.substring(0,fileName.lastIndexOf("(")) + ",";
                }
                for (var i = 0; i < $filesName.length; i++) {
                    names = names + $filesName[i].value + ",";
                }
                $("#fileIds_attach").val(ids);
                $("#fileNames_attach").val(names);
                haveUploaded_attach = true;
            },
            onUploadStart: function (file) {//上传开始时触发（每个文件触发一次）
                haveUploaded_attach = false;
                if ("no" != fileCount) {
                    if (parseInt(fileCount_attach) <= fileCountLimit) {
                        Jmts.alert("上传附件数超限制，最多上传" + fileCountLimit + "个附件");
                        $("#" + file.id).nextAll().each(function (index) {
                            handleCancelIduploadify_attach($(this).attr("id"), null, false);
                            $("#uploadify_attach").uploadify('cancel', $(this).attr("id"));
                        });
                        //取消上传
                        $("#uploadify_attach").uploadify('stop', file);
                        $("#uploadify_attach").uploadify('cancel', file.id);
                        handleCancelIduploadify_attach(file.id, file.size, false);
                        return;
                    }
                }
            }
        });
    });

    function handleCancelIduploadify_attach(swfFileId, swfFileSize, flag) {
        if (swfFileId && flag == undefined && $('#' + swfFileId).find('.data').html() == ' - 完成') {
            fileCount_attach = fileCount_attach - 1;
            totalFileSize = totalFileSize - swfFileSize;
            var deletedId = $("#" + swfFileId).find("input[name='successUploadFileId_attach']");
            $("#deletedIds_attach").val($("#deletedIds_attach").val() + deletedId.val() + ",");
        }
    }

    function deleteAttach(objectId, attachId) {
        Jmts.confirm('确定要删除吗？', function () {
            var url = "/systemAttach/deleteAttachRelation";
            var data = {
                objectId: objectId,
                attachId: attachId
            }
            Jmts.ajaxPost(url, data, function (result) {
                if (result.success) {
                    Jmts.succTips("删除成功");
                    window.location.reload();
                } else {
                    Jmts.warnTips(result.msg);
                }
            });
        });
    }
</script>
